#! /bin/bash
# 此程序用于版本发布

. ./scripts/shell/common.sh
. ./scripts/shell/golang.sh
. ./scripts/shell/tool.sh

PRE_HOOK="./scripts/hooks/pre-release.sh"
END_HOOK="./scripts/hooks/end-release.sh"

if [ -f ${PRE_HOOK} ];then
. ${PRE_HOOK}
fi

CHANGLOG_PATH=./CHANGELOG

printShort "正在计算版本号"

VERSION=`${GSEMVER} bump`

printLong "准备发布"

TAG=v${VERSION}

printShort "生成 ChangeLog"

${GIT} tag ${TAG}

${GIT_CHGLOG}  -o ${CHANGLOG_PATH}/${TAG}.md ${TAG}

${GIT} tag -d ${TAG}

# 提交更新日志
printShort "创建版本提交"
${GIT} add ${CHANGLOG_PATH}/*
${GIT} commit -m "version: ${TAG}"
${GIT} tag ${TAG}

printShort "推送到远端"
${GIT} push
${GIT} push --tags

if [ -f ${END_HOOK} ];then
. ${END_HOOK}
fi

if [[ "$?" != 0 ]]; then
    exit 1
fi